#pragma once

#include <cassert>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *lock_p = nullptr)
        : _lock_p(lock_p)
    {
    }

    void lock()
    {
        if (_lock_p)
        {
            int n = pthread_mutex_lock(_lock_p);
            assert(n == 0);
            (void)n;
        }
    }

    void unlock()
    {
        if (_lock_p)
        {
            int n = pthread_mutex_unlock(_lock_p);
            assert(n == 0);
            (void)n;
        }
    }

    ~Mutex()
    {
    }

private:
    pthread_mutex_t *_lock_p;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t *mutex) : _mutex(mutex)
    {
        _mutex.lock();
    }

    ~LockGuard()
    {
        _mutex.unlock();
    }

private:
    Mutex _mutex;
};